//
// Created by Hi on 25-7-23.
//

#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <condition_variable>
#include <functional>
#include <mutex>
#include <queue>
#include <thread>
#include <vector>


class ThreadPool {
private:
    std::vector<std::thread> m_threads;       // 线程池中的线程
    std::queue<std::function<void()>> m_tasks;  // 任务队列
    std::mutex m_taskMutex;       //互斥锁
    std::condition_variable m_cv;  //条件变量
    std::atomic<bool> m_stop;   //停止标志

    ThreadPool();
    ThreadPool(int size);
    ThreadPool(const ThreadPool&) = delete;
    ThreadPool& operator=(const ThreadPool&) = delete;

    // 静态单例相关成员
    static std::unique_ptr<ThreadPool> instance;
    static std::mutex instanceMutex;

public:
    // 添加任务
    void addTask(std::function<void()> task);
    static ThreadPool* getInstance();
    ~ThreadPool();
};



#endif //THREADPOOL_H
